/*** 
This do-file combines all ACS ZCTA-level variables into a single cleaned dataset.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

*-------------------------------------------------------------------------------
* Run cleaning code for each ACS variable
*-------------------------------------------------------------------------------

project, do("${root}/code/ACS 2014-2018 5-Year ZCTA/ACS 2014-2018 Median 2BR Rent - Cleaning.do")
project, do("${root}/code/ACS 2014-2018 5-Year ZCTA/ACS 2014-2018 Median Household Income - Cleaning.do")
project, do("${root}/code/ACS 2014-2018 5-Year ZCTA/ACS 2014-2018 Population - Cleaning.do")
project, do("${root}/code/ACS 2014-2018 5-Year ZCTA/2010 Census ZCTA Area - Cleaning.do")

* Combine ACS variables
project, uses("${root}/data/derived/ACS 2014-2018 5-Year ZCTA/Individual Variables/ACS 2014-2018 Population.dta")
use "${root}/data/derived/ACS 2014-2018 5-Year ZCTA/Individual Variables/ACS 2014-2018 Population.dta", clear

foreach file in "Median Household Income" "Median 2BR Rent" {
	project, uses("${root}/data/derived/ACS 2014-2018 5-Year ZCTA/Individual Variables/ACS 2014-2018 `file'.dta")
	merge 1:1 zcta5 using "${root}/data/derived/ACS 2014-2018 5-Year ZCTA/Individual Variables/ACS 2014-2018 `file'.dta", assert(3) nogen
}

project, uses("${root}/data/derived/ACS 2014-2018 5-Year ZCTA/Individual Variables/2010 Census ZCTA Area.dta")
merge 1:1 zcta5 using "${root}/data/derived/ACS 2014-2018 5-Year ZCTA/Individual Variables/2010 Census ZCTA Area.dta", assert(3) nogen

*-------------------------------------------------------------------------------
* Save final file
*-------------------------------------------------------------------------------

rename zcta5 zcta
save "${root}/data/derived/ACS 2014-2018 5-Year ZCTA/ACS 2014-2018 ZCTA.dta", replace
project, creates("${root}/data/derived/ACS 2014-2018 5-Year ZCTA/ACS 2014-2018 ZCTA.dta")
